package com.sky.controller.admin;

import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RequestMapping("/admin/dish")
@RestController("adminDishController")
@Slf4j
@Api(tags = "菜品相关功能")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     *
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishVO dishVO) {
        log.info("新增菜品{}", dishVO);

        dishService.saveDish(dishVO);

        //清理缓存数据
        String key = "dish_" + dishVO.getCategoryId();
        cleanCache(key);


        return Result.success();
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> DishPageQuery(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询参数:{}", dishPageQueryDTO);

        PageResult pageResult = dishService.DishPageQuery(dishPageQueryDTO);

        return Result.success(pageResult);
    }

    /**
     * 根据ID返回菜品数据
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID返回菜品数据")
    public Result<DishVO> queryDishById(@PathVariable Long id) {
        log.info("查询菜品id:{}", id);

        DishVO dishVO = dishService.queryDishById(id);

        return Result.success(dishVO);
    }

    /**
     * 修改菜品信息
     *
     * @param dishVO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品信息")
    public Result updateDish(@RequestBody DishVO dishVO) {
        log.info("修改菜品信息:{}", dishVO);
        dishService.updateDish(dishVO);

        //清理所有以dish_开头的缓存
        cleanCache("dish_*");

        return Result.success();
    }

    /**
     * 根据ids批量删除菜品
     *
     * @param ids
     */
    @DeleteMapping
    @ApiOperation("根据id批量删除菜品")
    public Result deleteDishByIds(Long[] ids) {
        log.info("删除的菜品ids:{}", ids);

        dishService.deleteDishByIds(ids);
        
        //清理所有以dish_开头的缓存
        cleanCache("dish_*");

        return Result.success();
    }

    @ApiOperation("根据id修改菜品状态")
    @PostMapping("/status/{status}")
    public Result starAndStop(@PathVariable Integer status, Long id) {
        log.info("修改的id为:{},修改后的状态:{}", id, status);

        dishService.starAndStop(status, id);

        //清理所有以dish_开头的缓存
        cleanCache("dish_*");

        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId) {

        List<Dish> list = dishService.list(categoryId);

        return Result.success(list);
    }
    private void cleanCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

}
